#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <string.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <sys/types.h>




int IC_TTY_addToLibIc()
{
	printf("IC_TTY_addToLibIc");
	
	return 0;
}

int IC_TTY_Option_Set(int fd,int nSpeed,int nBits,char nEvent,int nStop)
{
	struct termios newtio,oldtio;

	if(tcgetattr(fd,&oldtio)!=0)
	{
		printf("SetupSerial 1");
		return -1;
	}

	bzero(&newtio,sizeof(newtio));

	newtio.c_cflag |= CLOCAL |CREAD;
	newtio.c_iflag &= ~CSIZE;

	switch(nBits)
	{
		case 5:
			newtio.c_cflag |= CS5;
			break;
		case 6:
			newtio.c_cflag |= CS6;
			break;
		case 7:
			newtio.c_cflag |= CS7;
			break;
		case 8:
			newtio.c_cflag |=CS8;
			break;
		default:
			newtio.c_cflag |=CS8;
			break;
	}

	switch(nEvent)
	{
		case 'o':
			newtio.c_cflag |= PARENB;
			newtio.c_cflag |=PARODD;
			newtio.c_iflag |=(INPCK | ISTRIP);
			break;

		case 'E': 
			newtio.c_iflag |=(INPCK | ISTRIP);
			newtio.c_cflag |=PARENB;
			newtio.c_cflag &= ~PARODD;
			break;

		case 'N':
			newtio.c_cflag &= ~PARENB;
			break;
		default:
			newtio.c_cflag &= ~PARENB;
			break;

	}

	switch(nSpeed)
	{  
		case 2400:
			cfsetispeed(&newtio,B2400);
			cfsetospeed(&newtio,B2400);
			break;

		case 4800:
			cfsetispeed(&newtio,B4800);
			cfsetospeed(&newtio,B4800);
			break;

		case 9600:
			cfsetispeed(&newtio,B9600);
			cfsetospeed(&newtio,B9600);
			break;

		case 57600:
			cfsetispeed(&newtio,B57600);
			cfsetospeed(&newtio,B57600);
			break;

		case 115200:
			cfsetispeed(&newtio,B115200);
			cfsetospeed(&newtio,B115200);
			break;

		case 460800:
			cfsetispeed(&newtio,B460800);
			cfsetospeed(&newtio,B460800);
			break;

		default:
			cfsetispeed(&newtio,B9600);
			cfsetospeed(&newtio,B9600);
			break;

	}


	if(nStop == 1) 
	{
		newtio.c_cflag &= ~CSTOPB;
	}
	else if(nStop == 2)
	{
		newtio.c_cflag |= CSTOPB;
	}

	newtio.c_cc[VTIME] = 0; 
	newtio.c_cc[VMIN] = 0;

	tcflush(fd,TCIFLUSH);

	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		printf("com set error");
		return -1;
	}

	return 0;
}


int IC_TTY_Init(const char* pTtyName,int nSpeed,int nBits,char nEvent,int 
nStop)
{
	if (NULL == pTtyName)
	{
		printf("pTtyName is NULL");
		return -1;
	}
	
	int nComFd = 0;

	nComFd = open(pTtyName, O_RDWR|O_NDELAY|O_NOCTTY);
	if (nComFd <= 0 )
	{
		printf("Couldn't open %s", pTtyName);
		
		return -2;
	}
	else
	{
		printf("open %s success!", pTtyName);	
	}


	IC_TTY_Option_Set(nComFd,nSpeed,nBits,nEvent,nStop);

	fcntl( nComFd, F_SETFL, FNDELAY );

	return nComFd;
}

int IC_TTY_Destroy(int nComFd)
{
	if (nComFd > 0)
	{
		close(nComFd);
	}

	return 0;
}





int main(int argc,char**argv)
{
	int nSenDevFd = 0;

	if(argc!=2)
	{
		printf("Usage:[cmd] [dev]\n");
		exit(1);
	}
	nSenDevFd = IC_TTY_Init(argv[1], 9600, 8, 'N', 1);
	if (nSenDevFd < 0)
	{
		printf("error: open\n");
	}

	char szBuf[1024] = {0};
	int nRead = 0;

//	system("echo 1 > /sys/class/gpio/gpio49/value");
	while (1)
	{

		int ret = write(nSenDevFd, "abcdefghijklmnopqrstuvwxyz\r\n", 28);
		memset(szBuf, 0, 1024);
		/*
		nRead = read( nSenDevFd, szBuf, 1024);
		if (nRead > 0)
		{
			printf("recv %d data: %s", nRead, szBuf);
		}
		else
		{
			printf("read nothing\n");
		} */
		if(ret > 0)
		printf("send data %d\n",ret);
			sleep(1);
	}
	return 0;
}
